package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.MallDrawActivityChangePrizeCountRequest;
import com.sc.nft.admin.request.MallDrawActivityPrizePoolDetailRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.MallDrawActivityPrizePoolDetail;
import com.sc.nft.entity.dto.draw.activity.MallDrawActivityPrizePoolDTO;
import com.sc.nft.service.MallDrawActivityPrizePoolDetailService;
import com.sc.nft.service.MallDrawActivityPrizePoolService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/draw/activity/prize/pool")
@Api(tags = "抽奖活动-奖池模块")
public class DrawActivityPrizePoolController {
    private final MallDrawActivityPrizePoolService drawActivityPrizePoolService;
    private final MallDrawActivityPrizePoolDetailService mallDrawActivityPrizePoolDetailService;

    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "抽奖活动奖池列表", notes = "抽奖活动奖池列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<MallDrawActivityPrizePoolDTO>> list(@Valid @RequestBody IdRequest request) {
        return Result.success(drawActivityPrizePoolService.listByActivityIdToDTO(request.getId()));
    }

    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑奖品池", notes = "编辑奖品池", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result edit(@Valid @RequestBody MallDrawActivityPrizePoolDetailRequest request) {
        MallDrawActivityPrizePoolDetail mallDrawActivityPrizePoolDetail = new MallDrawActivityPrizePoolDetail();
        try {
            BeanUtils.copyProperties(mallDrawActivityPrizePoolDetail, request);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return Result.success(mallDrawActivityPrizePoolDetailService.update(mallDrawActivityPrizePoolDetail));
    }

    @PostMapping(value = "/change/prize/count", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑库存", notes = "编辑库存", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result changePrizeCount(@Valid @RequestBody MallDrawActivityChangePrizeCountRequest request) {
        return Result.success(mallDrawActivityPrizePoolDetailService.changeRemainingPrizeCountAndTotalPrizeCount(request.getAutoReplenishmentEnabled(), request.getAutoReplenishmentDTO(), request.getId(), request.getCount(), request.isDeduct()));
    }


}
